package main

import "fmt"

func main() {
	// 定义
	// 1. var identifier []type
	//  空切片  slice1 == nil  true
	var slice1 []int

	// 2. var slice1 []type = make([]type, len)
	var slice2 []int = make([]int, 10)

	// 3. slice1 := make([]type, len)
	slice3 := make([]int, 10)

	// 4. make([]T, length, capacity)
	slice4 := make([]int, 10, 20)

	fmt.Println("定义切片1：", slice1)
	fmt.Println("定义切片2：", slice2)
	fmt.Println("定义切片3：", slice3)
	fmt.Println("定义切片4：", slice4)

	fmt.Println("\n")

	arr := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	// 切片初始化
	// 1
	s1 := []int{1, 2, 3}

	// 2
	s2 := arr[0:5]

	// 3
	s3 := arr[0:5:8]

	// 4
	s4 := arr[:]

	// 5
	s5 := s4[:]

	// 6
	s6 := s4[1:3:3]

	fmt.Println("初始化切片1：", s1)
	fmt.Println("初始化切片2：", s2)
	fmt.Println("初始化切片3：", s3)
	fmt.Println("初始化切片4：", s4)
	fmt.Println("初始化切片5：", s5)
	fmt.Println("初始化切片6：", s6)

	fmt.Println("\n")

	// 验证操作s6 会不会改变s4 s5 arr
	// 结论：改变了 类型java sublist
	s6[0] = 1
	fmt.Println("修改6后观察4：", s4)
	fmt.Println("修改6后观察5：", s5)
	fmt.Println("修改6后观察6：", s6)
	fmt.Println("修改6后观察arr", arr)

}

func main_1() {
	a := [5]int{1, 2, 3, 4, 5}
	fmt.Printf("len(a) = %d, cap(a) = %d \n", len(a), cap(a))
	// len(a) = 5, cap(a) = 5

	b := []int{}
	b = append(b, 1, 2, 3, 4, 5)
	fmt.Printf("len(b) = %d, cap(b)= %d \n", len(b), cap(b))
	//	len(b) = 5, cap(b)= 6

	c := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	fmt.Printf("len(c) = %d, cap(c)= %d \n", len(c), cap(c))
	// len(c) = 9, cap(c)= 9

	var d []int
	d = append(d, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
	fmt.Printf("len(d) = %d, cap(d)= %d \n", len(d), cap(d))
	// len(d) = 10, cap(d)= 10

	// 总结：直接初始化后的，容量就是自身。 定义后直接append奇数个，cap=n+1， append偶数个，cap=n

	e := [...]int{}
	fmt.Printf("e type is %T \n", e)                            //[0]int  是个0数组
	fmt.Printf("len(e) is %d, cap(e) is %d \n", len(e), cap(e)) //len(e) is 0, cap(e) is 0

	f := [...]int{0, 1, 2, 3}
	fmt.Printf("f type is %T \n", f)                            //f type is [4]int
	fmt.Printf("len(f) is %d, cap(f) is %d \n", len(f), cap(f)) //len(f) is 4, cap(f) is 4

}
